<?php
namespace Swiftx\Component\Orm;
use Swiftx\Interfaces\Ioc\Container;

/**
 * 对象实体接口
 *
 * @author		Hismer <odaytudio@gmail.com>
 * @since		2014-12-16
 * @copyright	Copyright (c) 2014-2015 Swiftx Inc.
 */
class Searcher{

    /**
     * 注入容器
     * @var Container
     */
    protected $container;

    /**
     * 映射对象
     * @var array
     */
    protected $mapping = [];

    /**
     * 实体缓存
     * @var array
     */
    protected $entityCache = [];

    /**
     * 搜索器构造方法
     * @param Container $container
     */
    public function __construct(Container $container){
        $this->container = $container;
    }

    /**
     * 生成实体对象
     * @param string $entity
     * @param array $data
     * @return Entity
     */
    protected function buildObject(string $entity, array $data):Entity{
        /** @var Entity $object */
        $object = $this->container->fetch($entity);
        foreach($data as $key => $value){
            /** @var \ReflectionProperty $property */
            $property = $this->mapping['columns'][$key];
            $property->setValue($object, $value);
        }
        /** @var Table $table */
        $table = $this->mapping['table'];
        $columns = $table->getPrimary();
        $key = $entity;
        foreach($columns as $column)
            $key .= '::'.$data[$column];
        $this->entityCache[$key] = $object;
        $object->setLinked(true);
        return $object;
    }
}